/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.kaptcha.Constants;

import cn.cnsy123.bookwebsiteserver.cache.UserCacheInfo;
import cn.cnsy123.bookwebsiteserver.constant.CacheConstant;
import cn.cnsy123.bookwebsiteserver.entity.SysUser;
import cn.cnsy123.bookwebsiteserver.param.UserLoginParam;
import cn.cnsy123.bookwebsiteserver.properties.DefaultPropertiesConfig;
import cn.cnsy123.bookwebsiteserver.service.user.LoginService;
import cn.cnsy123.bookwebsiteserver.util.JsonUtil;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * 
 * @author yangpengfei
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;
    
    @Autowired
    private DefaultPropertiesConfig defaultPropertiesConfig;

    @GetMapping(value = "/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        modelAndView.setStatus(HttpStatus.OK);
        Map<String, String> modelMap = new HashMap<String,String>();
        modelMap.put("sys_name", defaultPropertiesConfig.getSystemName());
        modelAndView.addAllObjects(modelMap);
        return modelAndView;
    }
    
    @GetMapping(value = "/login")
    public String skipToLoginPage() {
        return "redirect:/loginPage";
    }

    @PostMapping(value = "/login")
    public Object doLogin(UserLoginParam userLoginParam, HttpServletRequest request) {
        String nowVerifyCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        Map<String, String> result = loginService.doLogin(userLoginParam, request.getRemoteHost(),nowVerifyCode);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(result.get("view"));
        modelAndView.setStatus(HttpStatus.OK);
        if ("login".equals(result.get("view"))) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("errorMessage", result.get("errorMessage"));
            map.put("sys_name", defaultPropertiesConfig.getSystemName());
            modelAndView.addAllObjects(map);
            return modelAndView;
        }else {
            cacheInfo(JsonUtil.jsonStr2Bean(result.get("cacheInfo"), SysUser.class),request);
            return "redirect:/indexPage";
        }
    }
    
    /**
     * Description:一些注释<br/>
     * CreateTime:2019-03-26<br/>
     * Creater: yangpengfei360@126.com <br/>
     * @author yangpengfei
     * @param sysUser
     */
    private void cacheInfo(SysUser sysUser,HttpServletRequest request) {
        UserCacheInfo cacheInfo = new UserCacheInfo();
        cacheInfo.setUserId(sysUser.getUserId());
        cacheInfo.setUserName(sysUser.getUserName());
        request.getSession().setAttribute(CacheConstant.SESSION_USER, cacheInfo);
    }

}
